package com.project.psc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.dto.SupplyRefundRecordDto;
import com.project.psc.bean.ReturnBean;
import com.project.psc.bean.SupplyBean;
import com.project.psc.mapper.IReturnMapper;
import com.project.psc.mapper.ISupplierMapper;
import com.project.psc.mapper.ISupplyMapper;
import com.project.psc.service.IReturnService;
import com.project.psc.util.ConvertStatus;
import com.project.psc.vo.ReturnVo;
import com.project.service.ISupplierService;
import com.project.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

@Service
@Transactional
public class ReturnServiceImpl implements IReturnService {
    @Autowired
    private IReturnMapper returnMapper;
    @Autowired
    private ISupplyMapper supplyMapper;
    @Autowired
    private ISupplierService supplierService;

    @Override
    public IPage<ReturnVo> find(Integer pageNO, Long supplierId) {
        IPage<ReturnBean> returnBeanPage = returnMapper.find(new Page<>(pageNO, 6), supplierId);

        Page<ReturnVo> copy = ConvertBeanUtil.copy(returnBeanPage, Page.class, (m, n) -> {
            n.setRecords(
                    ConvertBeanUtil.copyList(m.getRecords(), ReturnVo.class, (x, y) -> {
                        y.setStatus(ConvertStatus.orderStatus(x.getStatus()));
                    })
            );
        });

        return copy;
    }

    @Override
    public void updateStatus(Long id) {
        ReturnBean returnBean = returnMapper.selectById(id);
        returnBean.setStatus(2);
        returnMapper.updateById(returnBean);
        //TODO 调用后端接口修改他们的状态
        supplierService.updateSupplierStatus(returnBean.getReturnId(), 1);


    }

    //TODO 未测试
    @Override
    @RabbitListener(queues = "SupplyOrderRefundOrder")
    public void add(String json) {
        System.err.println(json);

        SupplyRefundRecordDto recordDto = JSONObject.parseObject(json, SupplyRefundRecordDto.class);
        QueryWrapper<ReturnBean> qw1 = new QueryWrapper<>();
        qw1.eq("r_returnId", recordDto.getRefundRef());
        boolean exists = returnMapper.exists(qw1);
        if (!exists){
            QueryWrapper<SupplyBean> qw = new QueryWrapper<>();
            qw.eq("s_batch", recordDto.getBatch());
            SupplyBean supplyBean = supplyMapper.selectOne(qw);

            ReturnBean returnBean = new ReturnBean();
            returnBean.setSupplierId(supplyBean.getSupplierId());
            returnBean.setReturnId(recordDto.getRefundRef());
            returnBean.setBatch(recordDto.getBatch());
            returnBean.setTime(recordDto.getDate());
            returnBean.setReason(recordDto.getReason());
            returnBean.setSumPrice(recordDto.getOrderMoney());
            returnBean.setStatus(1);

            returnMapper.insert(returnBean);
        }


    }
}
